<template>
	<div>
		<ProTable ref="proTable" title="抽奖数据" :columns="columns" :request-api="getTableList" :init-param="initParam">
			<!-- 表格 功能 按钮 -->
			<template #otherButton>
				<el-button @click="onExport" v-auth="'draw:activity:data:export:invoke'"> 导出 </el-button>
			</template>
			<!-- Expand -->
			<template #expand="scope">
				{{ scope.row }}
			</template>
		</ProTable>
	</div>
</template>

<script setup lang="tsx" name="lotteryData">
import { onMounted, ref, reactive, computed } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { NextLoading } from '/@/utils/loading';
import ProTable from '../../../../components/ProTable/index.vue';
import { ProTableInstance, ColumnProps } from '../../../../components/ProTable/interface';
import { fetchDataList, fetchExportData } from '/@/api/lottery';
import { useRouter, useRoute } from 'vue-router';
import { encryptPhoneNumber } from '/@/utils/commonFunction';
import { useDownload } from '/@/hooks/useDownload';
import default_photo from '/@/assets/images/default_photo.jpg';

const router = useRouter();
const route = useRoute();

// 获取 ProTable 元素，调用其获取刷新数据方法（还能获取到当前查询参数，方便导出携带参数）
const proTable = ref<ProTableInstance>();

// 如果表格需要初始化请求参数，直接定义传给 ProTable(之后每次请求都会自动带上该参数，此参数更改之后也会一直带上，改变此参数会自动刷新表格数据)
const initParam = reactive({
	searchType: 'ID',
});

// 如果你想在请求之前对当前请求参数做一些操作，可以自定义如下函数：params 为当前所有的请求参数（包括分页），最后返回请求列表接口
// 默认不做操作就直接在 ProTable 组件上绑定	:requestApi="getTableList"
const getTableList = (params: any) => {
	let data = {
		...params,
		raffleId: route.params.id,
	};

	return fetchDataList(data);
};

// 表格配置项
const columns: ColumnProps<any>[] = [
	{
		prop: 'keywords',
		label: '用户搜索',
		isShow: false,
		search: {
			span: 2,
			render: (scope: any) => {
				const slot = {
					prepend: () => (
						<el-select
							v-model={scope.searchParam.searchType}
							placeholder="Select"
							style="width: 100px"
							onChange={(value) => {
								scope.searchParam.searchType = value;
							}}
						>
							{searchArr.map((item) => (
								<el-option key={item.value} label={item.label} value={item.value} />
							))}
						</el-select>
					),
				};
				return <el-input v-model={scope.modelValue} v-slots={slot} placeholder={getPlaceholder.value} maxlength={50}></el-input>;
			},
		},
	},
	{
		prop: 'userId',
		label: '用户ID',
	},
	{
		prop: 'userinfo',
		label: '用户信息',
		minWidth: 200,
		render: (scope) => {
			return (
				<div class="flex-center">
					<el-image src={scope.row.avatar || default_photo} style="width: 40px; height: 40px;flex-shrink:0" fit="cover"></el-image>
					<div style="text-align: left;" class="ml10">
						<el-link type="primary" underline={false} onClick={() => router.push(`/user/detail/${scope.row.userId}`)}>
							{scope.row.nickname}
						</el-link>
						<div>{encryptPhoneNumber(scope.row.phone) || '-'}</div>
					</div>
				</div>
			);
		},
	},
	{
		prop: 'time',
		label: '抽奖时间',
		minWidth: 140,
	},
	{
		prop: 'type',
		label: '奖品类型',
		minWidth: 180,
		search: { el: 'select', props: { placeholder: '选择奖品类型', clearable: true, filterable: true } },
		enum: [
			// {
			// 	label: '积分',
			// 	value: '0',
			// },
			{
				label: '实物',
				value: '1',
			},
			{
				label: '谢谢惠顾',
				value: '2',
			},
		],
		render: (scope) => {
			return scope.row.type;
		},
	},
	{
		prop: 'name',
		label: '奖品名称',
		minWidth: 120,
	},
	{
		prop: 'status',
		label: '奖品状态',
		minWidth: 180,
		search: { el: 'select', props: { placeholder: '选择奖品状态', clearable: true, filterable: true } },
		enum: [
			{
				label: '待领取',
				value: '1',
			},
			{
				label: '已领取',
				value: '2',
			},
			{
				label: '待发货',
				value: '3',
			},
			{
				label: '已发货',
				value: '4',
			},
		],
		render: (scope) => {
			return scope.row.status;
		},
	},
];

const searchArr = [
	{
		label: '用户ID',
		value: 'ID',
	},
	{
		label: '用户昵称',
		value: 'NAME',
	},
	{
		label: '手机号',
		value: 'PHONE',
	},
];

const getPlaceholder = computed(() => {
	const target = searchArr.find((item) => {
		return item.value == proTable.value?.searchParam.searchType;
	});
	return target ? `输入${target?.label}搜索` : '';
});

// 保存并导出
const onExport = () => {
	ElMessageBox.confirm('确认导出数据?', '温馨提示', { type: 'warning' }).then(() =>
		useDownload(fetchExportData, '活动数据', { ...proTable.value?.searchParam, raffleId: route.params.id })
	);
};

// 页面加载时
onMounted(() => {
	NextLoading.done();
});
</script>

<style lang="scss">
.demo-tabs {
	.el-tabs__header {
		margin-bottom: 0;
	}
}
</style>
